<template>
  <transition name="fade">
    <div v-if="visible" class="modal-overlay" @click.self="handleCancel">
      <div class="modal">
        <div class="modal-header">
          <div class="modal-title">保存更改</div>
        </div>
        <div class="modal-body">
          <p class="modal-message">
            文档<span v-if="fileName" class="file-name">{{ fileName }}</span>已修改但未保存。是否保存？
          </p>
        </div>
        <div class="modal-footer">
          <button class="btn btn-primary" @click="handleSave">保存</button>
          <button class="btn btn-danger" @click="handleDiscard">不保存</button>
          <button class="btn btn-default" @click="handleCancel">取消</button>
        </div>
      </div>
    </div>
  </transition>
</template>

<script setup lang="ts">
import { onMounted, onBeforeUnmount } from 'vue'

const props = defineProps<{ 
  visible: boolean,
  fileName?: string
}>()
const emit = defineEmits<{
  (e: 'update:visible', v: boolean): void
  (e: 'action', r: 'save' | 'discard' | 'cancel'): void
}>()

const close = () => emit('update:visible', false)
const handleSave = () => { emit('action', 'save'); close() }
const handleDiscard = () => { emit('action', 'discard'); close() }
const handleCancel = () => { emit('action', 'cancel'); close() }

const onKeyDown = (e: KeyboardEvent) => {
  if (!props.visible) return
  if (e.key === 'Escape') {
    e.preventDefault()
    handleCancel()
  }
  if (e.key.toLowerCase() === 's' && (e.ctrlKey || e.metaKey)) {
    e.preventDefault()
    handleSave()
  }
}

onMounted(() => {
  window.addEventListener('keydown', onKeyDown)
})

onBeforeUnmount(() => {
  window.removeEventListener('keydown', onKeyDown)
})
</script>

<style scoped>
.fade-enter-active, .fade-leave-active { transition: opacity .15s ease; }
.fade-enter-from, .fade-leave-to { opacity: 0; }

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal {
  width: 420px;
  max-width: 90vw;
  background: var(--modal-bg, #fff);
  color: var(--modal-fg, #1f2937);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.15);
  overflow: hidden;
}

.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid rgba(0,0,0,0.06);
}

.modal-title {
  font-size: 16px;
  font-weight: 600;
}

.modal-body { padding: 16px 20px; }
.modal-message { margin: 0; line-height: 1.7; }
.file-name { font-weight: 600; margin: 0 6px; }

.modal-footer {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  padding: 14px 20px 18px;
  background: rgba(0,0,0,0.02);
  border-top: 1px solid rgba(0,0,0,0.06);
}

.btn { 
  padding: 8px 14px; 
  border-radius: 8px; 
  border: none; 
  cursor: pointer; 
  font-size: 14px; 
}
.btn-primary { background: #3b82f6; color: #fff; }
.btn-primary:hover { background: #2563eb; }
.btn-danger { background: #ef4444; color: #fff; }
.btn-danger:hover { background: #dc2626; }
.btn-default { background: #e5e7eb; color: #111827; }
.btn-default:hover { background: #d1d5db; }

/* 暗色模式适配 */
:host(.dark) .modal { 
  --modal-bg: #1f2937; 
  --modal-fg: #e5e7eb; 
}
</style>